﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace Framework.Message.SMS.CMPP.Commands
{
    public class ConnectRespCommand : Command
    {
        #region 变量与属性
        private ConnectRespStatus _Status;
        /// <summary>
        /// 状态
        /// </summary>
        public ConnectRespStatus Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        private byte[] _AuthenticatorISMG;
        /// <summary>
        /// ISMG认证码，用于鉴别ISMG。
        /// 其值通过单向MD5 hash计算得出，表示如下：
        /// AuthenticatorISMG =MD5（Status+AuthenticatorSource+shared secret），Shared secret 由中国移动与源地址实体事先商定，AuthenticatorSource为源地址实体发送给ISMG的对应消息CMPP_Connect中的值。
        /// 认证出错时，此项为空。
        /// </summary>
        public byte[] AuthenticatorISMG
        {
            get
            {
                if (_AuthenticatorISMG == null)
                {
                    this._AuthenticatorISMG = new byte[16];
                }
                return _AuthenticatorISMG;
            }
            set { _AuthenticatorISMG = value; }
        }

        private string _Version;
        /// <summary>
        /// 服务器支持的最高版本号，对于3.0的版本，高4bit为3，低4位为0
        /// </summary>
        public string Version
        {
            get { return _Version; }
            set { _Version = value; }
        }
        #endregion

        #region 构造函数
        public ConnectRespCommand()
        {
        }

        public ConnectRespCommand(byte[] bytes)
            : base(bytes)
        {
        }

        public ConnectRespCommand(byte[] bytes, int startIndex)
            : base(bytes, startIndex)
        {
        }

        public ConnectRespCommand(uint sequence)
            : base(sequence)
        {
        }
        #endregion

        public override CMPPCommandType CommandType
        {
            get { return CMPPCommandType.Connect_Resp; }
        }

        public override uint BodyLength
        {
            get { return 4 + 16 + 1; }
        }

        protected override void BytesTo(byte[] bytes, int startIndex)
        {
            int i = (int)CommandHeader.HeaderLength;
            base.BytesTo(bytes, startIndex);
            _Status = (ConnectRespStatus)ByteIntConvert.Bytes2UInt(bytes, i);
            i += 4;
            for (int j = 0; j < AuthenticatorISMG.Length; j++, i++)
            {
                AuthenticatorISMG[j] = bytes[i];
            }
            _Version = bytes[i].ToString();
        }

        public override byte[] ToBytes()
        {
            int i = (int)CommandHeader.HeaderLength;
            byte[] bytes = base.ToBytes();
            ByteIntConvert.UInt2Bytes((uint)_Status).CopyTo(bytes, i);
            i += 4;
            AuthenticatorISMG.CopyTo(bytes, i);
            i += AuthenticatorISMG.Length;
            bytes[i] = (byte)int.Parse(_Version);
            return bytes;
        }
    }
}
